import { Component } from '@angular/core';
import {
  NbSortDirection,
  NbSortRequest,
  NbTreeGridDataSource,
  NbTreeGridDataSourceBuilder
} from '@nebular/theme';

interface TreeNode<T> {
  data: T;
}
interface FSEntry {
  id: number;
  name: string;
  mail: string;
  age: number;
  action: string;
}

@Component({
  selector: 'tables-smart-table',
  templateUrl: './smart-table.component.html',
  styleUrls: ['./smart-table.component.scss']
})

export class SmartTableComponent {

  actionColumns = ['action'];
  defaultColumns = ['id', 'name', 'mail', 'age', 'action'];
  allColumns = [this.actionColumns, ...this.defaultColumns];

  dataSource: NbTreeGridDataSource<any>;

  sortColumn: string = '';
  sortDirection: NbSortDirection = NbSortDirection.NONE;

  constructor(private dataSourceBuilder: NbTreeGridDataSourceBuilder<any>) {
    this.dataSource = this.dataSourceBuilder.create(this.data);
  }

  ngOnInit(): void {}

  changeSort(sortRequest: NbSortRequest): void {
    this.dataSource.sort(sortRequest);
    this.sortColumn = sortRequest.column;
    this.sortDirection = sortRequest.direction;
  }

  getDirection(column: string): NbSortDirection {
    if (column === this.sortColumn) {
      return this.sortDirection;
    }
    return NbSortDirection.NONE;
  }

  private data: TreeNode<FSEntry>[] = [
    {
      data: { id: 1, name: 'aa', mail: 'aa@qq.com', age: 12, action: '<a>修改</a>' }
    },
    {
      data: { id: 2, name: 'bb', mail: 'aa@qq.com', age: 22, action: '<a>修改</a>' }
    },
    {
      data: { id: 3, name: 'cc', mail: 'aa@qq.com', age: 15, action: '<a>修改</a>' }
    },
    {
      data: { id: 4, name: 'dd', mail: 'aa@qq.com', age: 20, action: '<a>修改</a>' }
    },
    {
      data: { id: 5, name: 'ee', mail: 'aa@qq.com', age: 21, action: '<a>修改</a>' }
    }
  ]

}
